<?php
class Pageley_Commerce_Command_LogInCustomer extends Pageley_Controller_Command_TransactionalRead {

	private $username;
	private $password;

	public function setUsername($username) {
		$this->username = $username;
	}

	public function setPassword($password) {
		$this->password = $password;
	}

    protected function executeInternal() {
        $unitOfWork = $this->getUnitOfWork();
        if($this->username == null && $this->password == null) {
            throw new Pageley_Domain_Exception();
        }
        $query = new Pageley_Query_Basic(Pageley_Commerce_Domain_Customer::type(),
            new Pageley_Query_Condition_AndGroup(
                new Pageley_Util_ArrayMap(
	                new Pageley_Query_Condition_Equals("username", $this->username),
	                new Pageley_Query_Condition_Equals("password", Pageley_Serializer_Format::$HTTP->getSerializer()->encrypt($this->password))
                )
            ));
        $customers = $unitOfWork->query($query);
        if($customers->size() === 0) {
        	throw new Pageley_Domain_Exception(new Pageley_I18n_Message("error.login.failed"));
        }
        $this->getSession()->set(Pageley_Commerce_Session::CUSTOMER, $customers->get(0));
    }
}